package cn.jietuo.juc.c_013_reftypeAndThreadLocal;

import java.util.concurrent.TimeUnit;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2021/1/14
 * @description:
 */
public class ThreadLocalTest {

    static ThreadLocal<Person> threadLocal = new ThreadLocal<Person>();

    static class Person{

        private String name;

        public  Person(){

        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


    public static void main(String[] args) {
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("---1---" + threadLocal.get());
        }).start();

        new Thread(()->{
            Person person = new Person();
            person.setName("张三");
            threadLocal.set(person);
            System.out.println("---2---" + threadLocal.get().getName());
        }).start();
    }
}
